/*-----------------------------------------------------------------------------*/
/* ExternAdmin.java                                                            */
/* external administrator for ecom application                                 */
/* Fabienne Boyer - Didier Donsez may 2006                                     */
/*-----------------------------------------------------------------------------*/
package ecom.client;

import ecom.beans.entity.Accounts;
import ecom.beans.entity.Category;
import ecom.beans.entity.Clients;
import ecom.beans.entity.Produits;
import ecom.beans.entity.Productstore;
import ecom.beans.session.*;
import javax.transaction.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

// RMI administrator for the ecom application.
public class ExternAdmin {

    static public int SUCCESS = 1;
    static public int ERROR = -1;

    static private int createAccounts(EcomAdminRemote ecomAdminBean, Accounts accounts) {
        System.out.println("-- not yet implemented\n");
        return 0;
    }

    static private int createProductstore(EcomAdminRemote ecomAdminBean, Productstore productstore) {
        System.out.println("-- not yet implemented\n");
        return 0;
    }

    static private int createClients(EcomAdminRemote ecomAdminBean, Clients clients) {
         System.out.println("-- not yet implemented\n");
        return 0;
    }


    int accountId = 0;
    Context initialContext = null;
    UserTransaction utx = null;

    public static void main(String[] args) {
        try {

            System.out.println("Begining Client...");

            parseArgs(args);

            // get JNDI initial context
            Context initialContext = null;
            try {
                initialContext = new InitialContext();
            } catch (Exception e) {
                System.err.println("Cannot get initial context for JNDI: " + e);
                System.exit(2);
            }

            // We want to start transactions from client: get UserTransaction
            UserTransaction utx = null;
            try {
                utx = (UserTransaction) initialContext.lookup("UserTransaction");
            } catch (NamingException e) {
                System.err.println("Cannot lookup UserTransaction: " + e);
                System.exit(2);
            }

            EcomAdminRemote ecomAdminBean = null;
            EcomDataRemote ecomDataBean = null;
            try {
                ecomAdminBean = (EcomAdminRemote) initialContext.lookup(EcomAdminRemote.class.getName());
                System.out.println("Create of EcomAdminBean done");
                ecomDataBean = (EcomDataRemote) initialContext.lookup(EcomDataRemote.class.getName());
            } catch (NamingException e) {
                System.err.println("Cannot get EcomAdminBean : " + e);
                System.exit(2);
            }

            while (true) {
                System.out.println("------------------------------------------------------");
                System.out.println("Enter a command :");
                System.out.println("1 = create an account ");
                System.out.println("2 = create a productStore");
                System.out.println("3 = create a product ");
                System.out.println("4 = create a Customer");
                System.out.println("0 = quit the application ");

                int choice = Tx.readInt();

                utx.begin();
                if (choice == 1) {
                    createAccounts(ecomAdminBean,CommandLineReader.getAccounts());
                } else if (choice == 2) {
                    createProductstore(ecomAdminBean, CommandLineReader.getProductstore());
                } else if (choice == 3) {
                    Produits p = CommandLineReader.getProduits();
                    Category productCategory = null;
                    productCategory = ecomDataBean.getCategoryById(
                            new Long(CommandLineReader.getProductCategory(
                            ecomDataBean.getAllProductCategoryToString())));
                    p.setCategory(productCategory);
                    createProduct(ecomAdminBean, p);
                } else if (choice == 4) {
                    createClients(ecomAdminBean, CommandLineReader.getClients());
                } else if (choice == 0) {

                    System.out.println("-- not yet implemented\n");
                } else {
                    System.out.println("Bad choice");
                }

                utx.commit();
            }
        } catch (Exception e) {
            System.err.println("Client get an exception " + e);
            System.exit(2);
        }
    }

    /**
     * Parses an argument list for an acountId
     *
     * @param args[]            String array of arguments
     */
    static void parseArgs(String args[]) {
        System.out.println("ParseArgs not yet implemented");
    }

    static private int createAccount(EcomAdminRemote ecomAdminBean, Accounts account) {
        //ecomAdminBean.addUserAccount(idUser,account);
        System.out.println("-- not yet implemented\n");
        return 0;
    }

    static private int createProductStore(EcomAdminRemote ecomAdminBean, Productstore productStore) {
        System.out.println("-- not yet implemented\n");
        return 0;
    }

    static private int createProduct(EcomAdminRemote ecomAdminBean, Produits product) {
        System.out.println("-- not yet implemented\n");
        return 0;
    }

}
